<template>
  <div class="login-wrap">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <div class="login-form-title">欢迎登录！</div>
      <el-form-item
        prop="username"
        style="width:371px;margin-bottom: 30px;margin-top:30px"
      >
        <el-input
          v-model="loginForm.username"
          class="login-form-account"
          placeholder="请输入账户"
        />
      </el-form-item>
      <el-form-item
        prop="password"
        style="width:371px;margin-bottom: 30px;"
      >
        <el-input
          v-model="loginForm.password"
          class="login-form-password"
          type="password"
          placeholder="请输入密码"
        />
      </el-form-item>

      <div
        v-if="company_id != '4'"
        class="verification"
      >
        <el-input
          v-model="codeNum"
          auto-complete="off"
          placeholder="验证码"
          style="width: 371px"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img
            :src="codeImg"
            @click="getImg"
          >
        </div>
      </div>
      <el-button
        class="login-form-submit"
        @click="handleLogin()"
      >{{ !loading ? '立即登录' : '登 录 中...' }}</el-button>
    </el-form>
  </div>
</template>

<script>
import { encrypt, decrypt } from '@/utils/rsaEncrypt'
import { userLogin, getCodeImg } from '@/api/loginDefine'
import { setToken, setCompanyId } from '@/utils/auth'
import Config from '@/config'
import Cookies from 'js-cookie'
export default {
  name: 'UserLogin',
  props: {
    from: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      company_id: '',
      codeUrl: '',
      cookiePass: '',
      codeImg: '',
      codeNum: '',
      codeUUID: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, trigger: 'onBlur', message: '用户不能为空' }
        ],
        password: [
          { required: true, trigger: 'onBlur', message: '密码不能为空' }
        ]
      },
      loading: false,
      redirect: undefined
    }
  },
  created() {
    this.getCookie()
    console.log('process.env.company_id', process.env.company_id)
    this.company_id = process.env.company_id
    if (process.env.company_id !== 10001) {
      this.getImg()
    }
  },
  methods: {
    getImg() {
      getCodeImg().then(res => {
        this.codeImg = 'data:image/gif;base64,' + res.img
        this.codeUUID = res.uuid
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      let password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? '' : password
      password = password === undefined ? this.loginForm.password : password
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const [username, password] = [
            this.loginForm.username,
            encrypt(this.loginForm.password)
          ]
          userLogin(username, password, this.codeNum, this.codeUUID)
            .then(res => {
              if (res.code === '100000000000') {
                this.loading = false
                if (this.loginForm.rememberMe === true) {
                  Cookies.set('username', username, {
                    expires: Config.passCookieExpires
                  })
                  Cookies.set('password', password, {
                    expires: Config.passCookieExpires
                  })
                }
                // 设置统一的token和公司id
                setToken(res.data.token)
                setCompanyId(res.data.companycode)
                this.$store.dispatch('setLoadMenus', true)
                this.$router.push({ path: this.redirect || '/' })
                /* getRoleList(username).then(res => {
                this.$store.commit('setUserInfo', { roles: res })
              })*/
              } else {
                this.$notify({
                  title: res.msg,
                  type: 'error',
                  duration: 5000
                })
                this.loading = false
              }
            })
            .catch(error => {
              this.$notify({
                title: error,
                type: 'error',
                duration: 5000
              })
              this.loading = false
            })
        } else {
          console.log('error Login!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login-wrap {
  width: 477px!important;
  height: 458px!important;
  display: flex;
}
.login-form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-sizing: border-box;
  padding: 30px;
  box-sizing: border-box;
  &-title {
    color: #fff;
    font-size: 30px;
  }
  &-account input.el-input__inner,
  &-password input.el-input__inner {
    background-color: rgba(122, 126, 132, 0.2);
    border-radius: 4px;
    color: #9CAFEA!important;
    width: 371px!important;
  }

  input::-webkit-input-placeholder {
    color: #9CAFEA!important;
  }
  input::-moz-input-placeholder {
    color: #9CAFEA!important;
  }
  input::-ms-input-placeholder {
    color: #9CAFEA!important;
  }
  .verification {
    display: flex;
  }
  &-submit {
    font-weight: bold;
    color: #2B51CB;
    background: #fff;
    font-size: 18px;
    width: 371px;
  }
  .line-one {
    font-size: 14px;
    color: #333333;
    text-overflow: ellipsis;
    word-break: keep-all;
    white-space: nowrap;
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 0px 0px 15px;
    & div {
      width: 50%;
    }
    & div:nth-child(1) {
      text-align: left;
    }
    & div:nth-child(2) {
      text-align: right;
    }
  }
}
</style>
